package com.kirayous.blog.controller;


import com.kirayous.api.blog.service.ICategoryService;
import com.kirayous.common.Result;
import com.kirayous.common.annotation.OptLog;
import com.kirayous.common.annotation.PagePathPatternsAno;
import com.kirayous.common.constant.OptTypeConst;
import com.kirayous.common.vo.CategoryVO;
import com.kirayous.common.vo.ConditionVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author KiraYous
 * @since 2021-09-23
 */
@RestController
//@RequestMapping("/category")
public class CategoryController {



    @Autowired
    ICategoryService categoryService;

    /**
     * 搜索文章分类
     *
     * @param condition 条件
     * @return 分类列表
     */
    @ApiOperation(value = "搜索文章分类")
    @GetMapping("/admin/categories/search")
    public Result listCategoriesBySearch(ConditionVO condition) {
        return Result.success(categoryService.listCategoriesBySearch(condition));
    }


    /**
     * 查看后台分类列表
     *
     * @param condition 条件
     * @return  后台分类列表
     */
    @ApiOperation(value = "查看后台分类列表")
    @GetMapping("/admin/categories")
    @PagePathPatternsAno(path = "/admin/categories")
    public Result listBackCategories(ConditionVO condition) {
        return Result.success(categoryService.listBackCategories(condition));
    }

    /**
     * 添加或修改分类
     *
     * @param categoryVO 分类信息
     * @return {@link Result<>}
     */
    @OptLog(optType = OptTypeConst.SAVE_OR_UPDATE)
    @ApiOperation(value = "添加或修改分类")
    @PostMapping("/admin/categories")
    public Result saveOrUpdateCategory(@Valid @RequestBody CategoryVO categoryVO) {
        categoryService.saveOrUpdateCategory(categoryVO);
        return Result.success();
    }

    /**
     * 删除分类
     *
     * @param categoryIdList 分类id列表
     * @return {@link Result<>}
     */
    @OptLog(optType = OptTypeConst.REMOVE)
    @ApiOperation(value = "删除分类")
    @DeleteMapping("/admin/categories")
    public Result deleteCategories(@RequestBody List<Integer> categoryIdList) {
        categoryService.deleteCategory(categoryIdList);
        return Result.success();
    }

}

